<?php

class MY_form_validation extends CI_form_validation
{
	public function exist($str, $field)
	{
		sscanf($field, '%[^.].%[^.]', $table, $field);
		if (!isset($this->CI->db)) {
			throw new RuntimeException('此函数必须加载数据库类');
		}
		if ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() > 0) {
			return true;
		} else {
			$this->set_message('exist', '%s 不存在');
			return false;
		}
	}

	public function email_validate_code($str, $field)
	{
		sscanf($field, '%[^.]', $key);
		if (!isset($this->CI->session)) {
			throw new RuntimeException('此函数必须载入session库');
		}

		if ($this->CI->session->{$key} == $str) {
			return true;
		} else {
			$this->set_message('email_validate_code', '验证码不正确');
			return false;
		}
	}
}
